﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;

namespace KSUNO_EMS
{
    public class Uploader
    {
        public class Result
        {
            public bool success { get; set; }
            public string error { get; set; }
            public string filename { get; set; }
            public string fileExtension { get; set; }
        }

        public static Result GetFile(HttpRequestBase request, out byte[] data)
        {
            data = null;
            try
            {
                bool isFileStream = false;
                string fileName;

                fileName = request.Files[0].FileName;
                isFileStream = true;
                string fileExtension = Path.GetExtension(fileName);
                var uploadedStream = isFileStream ? request.Files[0].InputStream : request.InputStream;
                data = new byte[uploadedStream.Length];
                if ((uploadedStream.CanRead) && (uploadedStream.Length > 0))
                {
                    uploadedStream.Read(data, 0, data.Length);
                    return new Result { success = true, fileExtension = fileExtension, filename = fileName };
                }
            }
            catch (Exception ex)
            {
                return new Result { success = false, error = ex.ToString() };
            }
            return new Result { success = false, error = "Unable to read input stream." };
        }
        public static Result Save(HttpRequestBase request, string tempFolderPath,
                                  bool appendTimestampToFilename)
        {

            // string fileName = "";
            var tempDir = new DirectoryInfo(tempFolderPath);
            if (!tempDir.Exists)
                tempDir.Create();
            byte[] data;
            var result = GetFile(request, out data);

            result.filename = result.filename.Remove(result.filename.LastIndexOf(('.')));
            if (string.IsNullOrEmpty(result.filename))
                return new Result { success = false, error = "Request file is empty" };
            //return ("{success:false, error:'request file is empty'}");

            if (appendTimestampToFilename)
                result.filename += DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
            result.filename += result.fileExtension;

            try
            {
                using (
                    FileStream fileStream = new FileStream(Path.Combine(tempFolderPath, result.filename), FileMode.Create))
                    fileStream.Write(data, 0, data.Length);

            }
            catch (Exception ex)
            {
                return new Result { success = false, error = ex.ToString() };
                //return ("{success:false, error:'" + ex.Message + "'}");
            }
            return result;
            //return new Result { success = true, filename = fileName };
            // return ("{success:true, filename: '" + fileName + "'}");

        }
    }
}